 /* Server code in C */
//*
//*Date:2012-07-03
//*Author :CplusHua
//*服务端发送结构体数据
//*
//*
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <iostream>
#include <pwd.h>
#include <shadow.h>
using namespace std;
#include "mr_mem.h"
#include "sql_mr.h"
#include "server_hua.h"


using namespace std;

  int main(void)
  {
    signal(SIGPIPE,SIG_IGN);
    /*以下部分为测试服务器客户端连通性所返回的用户信息*/
	struct passwdsend p;
	struct spwdsend s;

	memset(&p,0x00,sizeof(struct passwd));


	strcpy(p.pw_name,"");
	cout<<p.pw_name<<endl;
	strcpy(p.pw_passwd,"x");
	p.pw_gid=0;
	p.pw_uid=0;
	strcpy(p.pw_shell,"/bin/bash");
	strcpy(p.pw_dir,"/home/gonghua");


	char sendbuff[sizeof(struct passwd)];
	memset(sendbuff,0,sizeof(sendbuff));
	memcpy(sendbuff,&p,sizeof(struct passwd));
	//socket
	//listen
    struct sockaddr_in stSockAddr;
    int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    if(-1 == SocketFD)
    {
      perror("can not create socket");
      exit(EXIT_FAILURE);
    }

    memset(&stSockAddr, 0, sizeof(struct sockaddr_in));

    stSockAddr.sin_family = AF_INET;
    stSockAddr.sin_port = htons(5555);
    stSockAddr.sin_addr.s_addr = INADDR_ANY;

    if(-1 == bind(SocketFD,(const struct sockaddr *)&stSockAddr, sizeof(struct sockaddr_in)))
    {
      perror("error bind failed");
      close(SocketFD);
      exit(EXIT_FAILURE);
    }

    if(-1 == listen(SocketFD, 10))
    {
      perror("error listen failed");
      close(SocketFD);
      exit(EXIT_FAILURE);
    }
    cout<<"I Am Wating Client .............."<<endl;




    for(;;)
    {
      int ConnectFD = accept(SocketFD, NULL, NULL);
      if(0 > ConnectFD)
      {
       perror("error accept failed");
       close(SocketFD);
       exit(EXIT_FAILURE);
      }
       struct RequInfo Req;

    memset(&Req,0x00,sizeof(struct RequInfo));

    read(ConnectFD,&Req,sizeof(Req));


    char temp_hua_12[100];
    strcpy(temp_hua_12,Req.Machname);
    temp_hua_12[strlen(Req.Machname)-1]='\0';
    //for test
    cout<<"该字串的长度为"<<strlen(Req.Machname)<<endl;
    cout<<"new lengh"<<strlen(temp_hua_12)<<endl;

    strcpy(Req.Machname,temp_hua_12);
	//for test
	cout<<"打印发来的参数"<<endl;
	char testss[sizeof(RequInfo)];
	memcpy(&testss,&Req,sizeof(testss));
    for(int iii=0;iii<sizeof(RequInfo);iii++)
			cout<<testss[iii];cout<<endl;

    cout<<"RequInfo is :"<<Req.RequInfo<<endl;
    cout<<"Param is："<<Req.Param<<endl;
    cout<<"Machname is:"<<Req.Machname<<endl;
    char nss_status_back[2];
    //for test
	cout<<Req.RequInfo<<endl;

    if(!strcmp(Req.RequInfo,"USER"))
    {
        if(1==query_name(Req.Param ,Req.Machname))
            {
            cout<<"如何显示了这句话，则说明该用户可以登录该机器"<<endl;
                strcpy(nss_status_back,"o");
                //调用函数查询用户详细信息，返回结构体
		//memcached
                if(!mr_memgetp(Req,&p))
		{
			query_user(Req.Param,&p);
			mr_memsetp(Req ,&p);
		}
		else
		{
			cout<<"*****************************************数据来自memcached********************************************"<<endl;
		}
		//for test
		cout<<"数据库查询结果（返回到结构体P中的uid)"<<p.pw_uid<<endl;
                //回传数据
                senddata(p,nss_status_back,ConnectFD);
                memset(&p,0x00,sizeof(p));
                shutdown(ConnectFD, SHUT_RDWR);
                close(ConnectFD);
            }
        else
        {
            strcpy(nss_status_back,"n");
            memset(&p,0x00,sizeof(struct passwd));
            senddata(p,nss_status_back,ConnectFD);
            shutdown(ConnectFD, SHUT_RDWR);
            close(ConnectFD);

        }


    }
//cout<<"这里将要执行UID判断"<<endl;
    if(!strcmp(Req.RequInfo,"UID"))
	{
	   //memcached
               if(!mr_memgetp(Req,&p))
		{
	                 query_user(Req.Param,&p);
		         mr_memsetp(Req ,&p);
                }
                else
                {
                        cout<<"*****************************************数据来自memcached********************************************"<<endl;
                }
			strcpy(nss_status_back,"o");
			query_uid(Req.Param,&p);
			cout<<p.pw_uid<<endl;
			senddata(p,nss_status_back,ConnectFD);
                	shutdown(ConnectFD, SHUT_RDWR);
                	close(ConnectFD);

		}

//cout<<"这里执行完UID判断"<<endl;

    if ((!strcmp(Req.RequInfo,"USHADOW")))
    {
    //	cout<<"预测是0"<<strcmp(Req.RequInfo,"USHADOW")<<endl;
    //
    //	cout<<"sdfasdf"<<Req.RequInfo<<Req.Machname<<endl;
    //	cout<<"预测是1"<<query_name(Req.Param ,Req.Machname);}//Req.Machname);}
    //	strcpy(Req.Machname,Req.Machname);
     if (1==query_name(Req.Param ,Req.Machname))
        {
            cout<<"如果能看到这句话，说明已经进入USHADOW的IF"<<endl;
            strcpy(nss_status_back,"o");
          if( !mr_memgetsp(Req,&s))
            {
                query_uspwd(Req.Param,&s);
                 mr_memsetsp(Req ,&s);
            }
            else
            {

                cout<<"*****************************************数据来自memcached********************************************"<<endl;

            }
            sendspwd(s,nss_status_back,ConnectFD);
            shutdown(ConnectFD, SHUT_RDWR);
            close(ConnectFD);
        }
    else
        {
            strcpy(nss_status_back,"n");
    //                        query_uspwd(Req.Param,&s);
            if( !mr_memgetsp(Req,&s))
                         query_uspwd(Req.Param,&s);
                        else
                        {
                                mr_memsetsp(Req ,&s);
                                cout<<"数据来自memcached"<<endl;
                        }
                        //for test
                        cout<<"这里将会显示返回的SHADOW密码，如果不是则query_userspwd函数内部可能存在问题"<<s.sp_pwdp<<endl;
                        sendspwd(s,nss_status_back,ConnectFD);
                        shutdown(ConnectFD, SHUT_RDWR);
                        close(ConnectFD);
        }
    }

}
    close(SocketFD);
    return 0;
  }
